package cn.edu.gsau.info.controller;


import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.IShenheService;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.service.IZhuanlizhuanzhuService;
import cn.edu.gsau.info.util.UploadUtil;
import cn.edu.gsau.info.vo.ZhuanlizhuanzhuRules;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: ZhuanlzhuanzhuController
 * @BelongsPackage: cn.edu.gsau.info.controller
 * @Author: qsy
 * @CreateTime: 2022/11/19
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/zhuanlizhuanzhu")
public class ZhuanlizhuanzhuController {

    @Autowired
    IZhuanlizhuanzhuService iZhuanlizhuanzhuService;
    @Autowired
    IZhengmingcailiaoService iZhengmingcailiaoService;

    @Autowired
    IShenheService iShenHeService;

    @Value("${gsau.upload.path}")
    private String uploadfile;


    /**
     * @description:
     * @author: qsy
     * @date: 2022/11/26 11:45
     * @param: [file, request]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        User user=new User();
        user.setUserName("qiaoshiyi");
        UploadUtil uploadUtil=new UploadUtil();
        Map<String, String> map = uploadUtil.filedUpload(file,user.getUserName(), uploadfile);

        return new ResponseResult( map!=null? Code.SAVE_OK:Code.SAVE_ERR,map!=null?"保存成功":"保存失败", map);
    }

    /**
     * @description: 填报专利专著以及认证材料
     * @author: qsy
     * @date: 2022/11/26 14:55
     * @param: [form]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Zhuanlizhuanzhu form) {
        User user =new User();
        user.setId(8l);
        user.setNickName("谯诗意");
        user.setUserName("qiaoshiyi");
        boolean save=iZhuanlizhuanzhuService.addInfo(user,form);
        return new ResponseResult(save ==true ? Code.SAVE_OK : Code.SAVE_ERR, save ==true ? "添加成功" : "添加失败", null);
    }
    /**
     * @description: 根据id删除对应单条证明材料
     * @author: qsy
     * @date: 2022/11/26 19:02
     * @param:
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @DeleteMapping("/deletezhengming/{id}")
    public ResponseResult deleteZhengming(@PathVariable long id){
        boolean delete=iZhengmingcailiaoService.removeById(id);
        return new ResponseResult(delete == true ? Code.DELETE_OK: Code.DELETE_ERR, delete == true ? "删除成功" : "删除失败", delete);
    }

    /**
     * @description: 根据专利专著修改信息以及材料
     * @author: qsy
     * @date: 2022/11/27 20:03
     * @param: [form]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Zhuanlizhuanzhu form) {
        User user =new User();
        user.setId(8l);
        user.setNickName("谯诗意");
        user.setUserName("qiaoshiyi");
        long id=form.getId();
        Date date=new Date();
        form.setUpdateTime(date);
        Zhuanlizhuanzhu zhuanlizhuanzhu=iZhuanlizhuanzhuService.updateInfo(user,form,id);
        return new ResponseResult(zhuanlizhuanzhu != null ? Code.UPDATE_OK : Code.UPDATE_ERR, zhuanlizhuanzhu != null ? "修改成功" : "修改失败", zhuanlizhuanzhu);
    }

    /**
     * @description: 根据id主键值查询单条专利或软著
     * @author: qsy
     * @date: 2022/11/21 17:34
     * @param:[id]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @GetMapping("/findbyid/{id}")
    public ResponseResult findById(@PathVariable long id) {
        Zhuanlizhuanzhu zhuanlizhuanzhu = iZhuanlizhuanzhuService.getById(id);
        return new ResponseResult(zhuanlizhuanzhu != null ? Code.GET_OK : Code.GET_ERR, zhuanlizhuanzhu != null ? "查询成功" : "查询失败", zhuanlizhuanzhu);
    }
    /**
     * @description: 根据专利id查询证明材料
     * @author: qsy
     * @date: 2022/11/26 15:40
     * @param:[id]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @GetMapping("/findzhengmingbyid/{id}")
    public ResponseResult findZhengmingById(@PathVariable long id) {
        int moduleId = ModuleIdClass.ZHUANLIYURUANJIAN_id;
        List<Zhengmingcailiao> zhengmingcailiaos=iZhengmingcailiaoService.selectZhengmingcailiao(moduleId,id);
        return new ResponseResult(zhengmingcailiaos != null ? Code.DELETE_OK : Code.DELETE_ERR, zhengmingcailiaos != null ? "查询成功" : "查询失败", zhengmingcailiaos);
    }


    /**
     * @description:登录后，根据用户id和流程状态分页查询专利软著
     * @author: qsy
     * @date: 2022/11/23 19:23
     * @param: [rules]
     * @return:cn.edu.gsau.info.models.ResponseResult
     */
    @PostMapping("/findallbystatus")
    public ResponseResult findAllByStatus(@RequestBody ZhuanlizhuanzhuRules rules) {
        IPage iPage = iZhuanlizhuanzhuService.selectZhuanlizhuanzhuByStatus(rules);
        return new ResponseResult(iPage != null ? Code.GET_OK : Code.GET_ERR, iPage != null ? "查询成功" : "查询失败", iPage);
    }
    /**
     * @description:登录后，根据用户id和节点分页查询专利软著
     * @author: qsy
     * @date: 2022/11/23 19:23
     * @param: [rules]
     * @return:cn.edu.gsau.info.models.ResponseResult
     */
    @PostMapping("/findallbynode")
    public ResponseResult findAllByNode(@RequestBody ZhuanlizhuanzhuRules rules) {
        IPage iPage = iZhuanlizhuanzhuService.selectZhuanlizhuanzhuByNode(rules);
        return new ResponseResult(iPage != null ? Code.GET_OK : Code.GET_ERR, iPage != null ? "查询成功" : "查询失败", iPage);
    }

    /**
     * @description: 根据id删除对应专利或软件
     * @author: qsy
     * @date: 2022/11/23 11:30
     * @param: [id]
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteById(@PathVariable long id) {
        Boolean delete = iZhuanlizhuanzhuService.removeById(id);
        return new ResponseResult(delete == true ? Code.DELETE_OK: Code.DELETE_ERR, delete == true ? "删除成功" : "删除失败", delete);
    }

    /**
     * @description: 根据专利id查询审核流程
     * @author: qsy
     * @date: 2022/11/28 10:30
     * @param: 无
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @GetMapping("/findshenhe/{id}")
    public ResponseResult findShenHe(@PathVariable long id){
        List<Shenhe> list=iShenHeService.selectShenHe(id);
        return new ResponseResult(list != null ? Code.GET_OK : Code.GET_ERR, list != null ? "查询成功" : "查询失败", list);
    }

    /**
     * @description: 根据专利id添加审核流程
     * @author: qsy
     * @date: 2022/11/28 10:30
     * @param: 无
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @PostMapping("/addshenhe")
    public ResponseResult updateShenHe(@RequestBody Shenhe shenHe){
        boolean result=iZhuanlizhuanzhuService.addShenHe(shenHe);
        return new ResponseResult(result ==true ? Code.UPDATE_OK: Code.UPDATE_ERR, result ==true? "更新成功" : "更新失败", result);
    }

    /**
     * @description: 根据项目id查询最后一条审核记录
     * @author: qsy
     * @date: 2022/11/29 18:08
     * @param: 无
     * @return: cn.edu.gsau.info.models.ResponseResult
     */
    @GetMapping("/findendshenhe/{id}")
    public ResponseResult findEndShenHe(@PathVariable long id){
        Shenhe shenHe=iZhuanlizhuanzhuService.findEndShenHe(id);
        return new ResponseResult(shenHe!=null? Code.GET_OK: Code.GET_ERR, shenHe !=null? "查询成功" : "查询失败", shenHe);
    }
}